<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>空值合并</title>
</head>

<body> 
    <script>                
        let number     
        // ||操作符右边的值仅在左边的值为假值时有效
        number = 1
        let myNumber = number || 7 // 1为真 
        console.log(myNumber); // 1
        number = 0
        myNumber = number || 7 // 0为假
        console.log(myNumber); // 7
        number = 12
        myNumber = number || 7 // 0为假
        console.log(myNumber); // 7
        // ??操作符右边的值仅在左边的值等于 null 或 undefined 时有效
        number = 0
        myNumber = number ?? 7
        console.log(myNumber); // 0
        number = 1
        myNumber = number ?? 7
        console.log(myNumber); //1
        number = null
        myNumber = number ?? 7
        console.log(myNumber); //7
    </script>
</body>

</html>